<template>
  <el-container class="flow-info-container">
    <el-main>
      <div style="position: relative; width:100%">
        <el-form ref="formFlowCategory" :model="formFlowCategory" label-width="100px" size="mini" label-position="right" @submit.native.prevent>
          <FilterBox :item-width="550">
            <el-row :gutter="40">
              <el-col :span="12">
                <el-form-item label="分类名称" prop="formFilter.name">
                  <el-input v-model="formFlowCategory.formFilter.name" class="filter-item" :clearable="true" placeholder="流程分类名称" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="分类编码" prop="formFilter.code">
                  <el-input v-model="formFlowCategory.formFilter.code" class="filter-item" :clearable="true" placeholder="分类编码" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-button slot="operator" type="default" :plain="true" size="mini" @click="onReset">重置</el-button>
            <el-button slot="operator" type="primary" :plain="true" size="mini" @click="refreshFormFlowCategory(true)">查询</el-button>
          </FilterBox>
        </el-form>
        <el-card shadow="never" class="tableCard">
          <el-row>
            <el-col :span="24">
              <vxe-toolbar :refresh="{query: refresh}">
                <template v-slot:buttons>
                  <el-button type="primary" icon="el-icon-plus" size="small" @click="onAddFlowCategoryClick()">新建</el-button>
                </template>
              </vxe-toolbar>
              <el-table
                ref="flowCategory"
                border
                max-height="450"
                :header-cell-style="{background:'#f4f4f4', color:'#717171' }"
                :data="flowCategoryWidget.dataList"
                size="middle"
                header-cell-class-name="table-header-gray"
                @sort-change="flowCategoryWidget.onSortChange"
              >
                <el-table-column label="序号" header-align="center" align="center" type="index" width="55px" :index="flowCategoryWidget.getTableIndex" />
                <el-table-column label="流程分类名称" prop="name" />
                <el-table-column label="分类编码" prop="code" />
                <el-table-column label="显示顺序" prop="showOrder" sortable="custom" />
                <el-table-column label="创建时间" prop="createTime" sortable="custom">
                  <template slot-scope="scope">
                    <span>{{ formatDateByStatsType(scope.row.createTime, 'day') }}</span>
                  </template>
                </el-table-column>
                <el-table-column label="操作" fixed="right" width="120px">
                  <template slot-scope="scope">
                    <el-button class="table-btn success" type="text" size="small" @click.stop="onEditFlowCategoryClick(scope.row)">编辑</el-button>
                    <el-button class="table-btn delete" type="text" size="small" @click.stop="onDeleteFlowCategoryClick(scope.row)">删除</el-button>
                  </template>
                </el-table-column>
              </el-table>
              <el-row type="flex" justify="end" style="margin-top: 10px;">
                <el-pagination
                  :total="flowCategoryWidget.totalCount"
                  :current-page="flowCategoryWidget.currentPage"
                  :page-size="flowCategoryWidget.pageSize"
                  :page-sizes="[10, 20, 50, 100]"
                  layout="total, prev, pager, next, sizes"
                  @current-change="flowCategoryWidget.onCurrentPageChange"
                  @size-change="flowCategoryWidget.onPageSizeChange"
                />
              </el-row>
            </el-col>
          </el-row>
        </el-card>
      </div>
    </el-main>
  </el-container>
</template>

<script>
/* eslint-disable-next-line */
import rules from '@/utils/validate.js'
/* eslint-disable-next-line */
import { DropdownWidget, TableWidget, UploadWidget, ChartWidget } from '@/utils/widget.js'
/* eslint-disable-next-line */
import { uploadMixin, statsDateRangeMixin, cachePageMixin } from '@/core/mixins'
/* eslint-disable-next-line */
import { FlowCategoryController } from '@/api/flowController.js'
import formEditFlowCategory from '@/views/workflow/flowCategory/formEditFlowCategory.vue'
import FilterBox from '@/components/FilterBox'
import '@/components/Dialog'

export default {
  name: 'FormFlowCategory',
  components: {
    FilterBox
  },
  mixins: [uploadMixin, statsDateRangeMixin, cachePageMixin],
  props: {
  },
  data() {
    return {
      formFlowCategory: {
        formFilter: {
          name: undefined,
          code: undefined
        },
        formFilterCopy: {
          name: undefined,
          code: undefined
        },
        isInit: false
      },
      flowCategoryWidget: new TableWidget(this.loadFlowCategoryWidgetData, this.loadFlowCategoryVerify, true, false, 'showOrder', 1)
    }
  },
  watch: {
  },
  mounted() {
    // 初始化页面数据
    this.formInit()
  },
  methods: {
    onReset() {
      this.$refs.formFlowCategory.resetFields()
      this.refreshFormFlowCategory(true)
    },
    /**
     * FlowCategory数据获取函数，返回Promise
     */
    loadFlowCategoryWidgetData(params) {
      if (params == null) params = {}
      params = {
        ...params,
        flowCategoryDtoFilter: {
          name: this.formFlowCategory.formFilterCopy.name,
          code: this.formFlowCategory.formFilterCopy.code
        }
      }
      return new Promise((resolve, reject) => {
        FlowCategoryController.list(params).then(res => {
          resolve({
            dataList: res.data.dataList,
            totalCount: res.data.totalCount
          })
        }).catch(e => {
          reject(e)
        })
      })
    },
    /**
     * FlowCategory数据获取检测函数，返回true正常获取数据，返回false停止获取数据
     */
    loadFlowCategoryVerify() {
      this.formFlowCategory.formFilterCopy.name = this.formFlowCategory.formFilter.name
      this.formFlowCategory.formFilterCopy.code = this.formFlowCategory.formFilter.code
      return true
    },
    /**
     * 更新流程分类管理
     */
    refreshFormFlowCategory(reloadData = false) {
      if (reloadData) {
        this.flowCategoryWidget.refreshTable(true, 1)
      } else {
        this.flowCategoryWidget.refreshTable()
      }
      if (!this.formFlowCategory.isInit) {
        // 初始化下拉数据
      }
      this.formFlowCategory.isInit = true
    },
    /**
     * 新建
     */
    onAddFlowCategoryClick() {
      const params = {}

      this.$dialog.show('新建', formEditFlowCategory, {
        area: '500px'
      }, params).then(res => {
        this.refreshFormFlowCategory()
      }).catch(e => {})
    },
    /**
     * 编辑
     */
    onEditFlowCategoryClick(row) {
      const params = {
        categoryId: row.categoryId
      }

      this.$dialog.show('编辑', formEditFlowCategory, {
        area: '500px'
      }, params).then(res => {
        this.flowCategoryWidget.refreshTable()
      }).catch(e => {})
    },
    /**
     * 删除
     */
    onDeleteFlowCategoryClick(row) {
      if (
        row.categoryId == null
      ) {
        this.$message.error('请求失败，发现必填参数为空！')
        return
      }
      const params = {
        categoryId: row.categoryId
      }

      this.$confirm('是否删除此流程分类？').then(res => {
        FlowCategoryController.delete(params).then(res => {
          this.$message.success('删除成功')
          this.flowCategoryWidget.refreshTable()
        }).catch(e => {})
      }).catch(e => {})
    },
    refresh() {
      this.refreshFormFlowCategory(true)
    },
    onResume() {
      this.refreshFormFlowCategory()
    },
    initFormData() {
    },
    formInit() {
      this.refreshFormFlowCategory()
    }
  }
}
</script>

<style  lang="scss">
  .flow-info-container {
    display: flex;

    flex-direction: column;
    margin: 0 auto;
    .blueLink {
      color: #409eff;
      font-size: 14px;
      margin-left: 20px;
      cursor: pointer;
    }
    .btnList {
      margin-top:20px;
      display: flex;
      justify-content: right;
      float: right;
      .submitBtn {
        margin-right: 30px;
      }
    }
    .fromFlex {
      height: 36px;
      display: flex;
      align-items: center;
    }
    .base-info {
      display: flex;
      margin-bottom: 20px;
      .basics-info {
        width: 100%;
      }
    }
  }
</style>
